package com.lifesaftypower.entity;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.res.AssetManager;

import com.lifesaftypower.MainApp;

public class Product {
	private String _model;
	private String _sector;
	private String _name;
	private String _description;
	private String _spec;
	public String getSpec() {
		return _spec;
	}

	public void setSpec(String spec) {
		_spec = spec;
	}

	private String _overview;
	private String _document;
	private String _support;
	private String _tool;
	private static List<Product> _productList;

	public static Product[] SampleData = new Product[] {
			new Product("model1", "market1", "name1", "description1",
					"overview", "documents", "support", "tools"),
			new Product("model2", "market1", "name2", "description2",
					"overview", "documents", "support", "tools"),
			new Product("model2", "market2", "name3", "description2",
					"overview", "documents", "support", "tools"),
			new Product("model2", "market2", "name4", "description2",
					"overview", "documents", "support", "tools"),
			new Product("model2", "market3", "name5", "description2",
					"overview", "documents", "support", "tools"),
			new Product("model3", "market4", "name6", "description3",
					"overview", "documents", "support", "tools"),
			new Product("model4", "market4", "name6", "description3",
					"overview", "documents", "support", "tools"),
			new Product("model4", "market5", "name6", "description3",
					"overview", "documents", "support", "tools"),
			new Product("model5", "market5", "name6", "description3",
					"overview", "documents", "support", "tools"),
			new Product("model6", "market7", "name6", "description3",
					"overview", "documents", "support", "tools"),
			new Product("model6", "market7", "name6", "description3",
					"overview", "documents", "support", "tools"),
			new Product("model7", "market8", "name6", "description3",
					"overview", "documents", "support", "tools"),
			new Product("model7", "market8", "name6", "description3",
					"overview", "documents", "support", "tools") };

	public static Product[] getBySector(String marketSector) {
		ArrayList<Product> productList = new ArrayList<Product>();
		for (Product p : getProductList()) {
			if (p.getMarketSector().equals(marketSector)) {
				productList.add(p);
			}
		}
		return productList.toArray(new Product[] {});
	}

	public static String[] getSector() {
		HashSet<String> marketSectorSet = new HashSet<String>();
		for (Product p : getProductList()) {
			marketSectorSet.add(p.getMarketSector());
		}

		return marketSectorSet.toArray(new String[] {});
	}

	public static List<Product> getProductList() {
		if (_productList == null) {
			_productList = Load();
		}
		return _productList;
	}

	private static List<Product> Load() {
		AssetManager assetManager = MainApp.getAppContext().getAssets();
		ArrayList<Product> result = new ArrayList<Product>();
		try {
			InputStream ins = assetManager.open("product.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder;

			dBuilder = dbFactory.newDocumentBuilder();

			Document doc = dBuilder.parse(ins);
			NodeList products = doc.getElementsByTagName("product");
			
			for (int i = 0; i < products.getLength(); i++) {
				Node productNode = products.item(i);
				Product product = new Product(productNode);
				result.add(product);
			}			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	private static String getNodeValue(Element elem, String nodeName) {
		String result = "";
		NodeList valueNodes = elem.getElementsByTagName(nodeName);
		if (valueNodes != null && valueNodes.getLength() > 0) {
			NodeList cdNodes = valueNodes.item(0).getChildNodes();

			for (int i = 0; i < cdNodes.getLength(); i++) {
				if (Node.CDATA_SECTION_NODE == cdNodes.item(i).getNodeType()) {
					result = cdNodes.item(i).getNodeValue();
					break;
				}
			}
		}
		return result;
	}

	public Product(Node xmlNode) {
		if (xmlNode.getNodeType() == Node.ELEMENT_NODE) {
			Element elem = (Element) xmlNode;
			_model = elem.getAttribute("model");
			_sector = elem.getAttribute("sector");
			_name = elem.getAttribute("name");
			_description = elem.getAttribute("description");

		
			_spec = getNodeValue(elem, "spec");
			_overview = getNodeValue(elem, "overview");
			_support = getNodeValue(elem, "support");
			_document = getNodeValue(elem, "document");

		}
	}

	public Product(String model, String marketSector, String name,
			String description, String overview, String documents,
			String support, String tools) {
		super();
		_model = model;
		_sector = marketSector;
		_name = name;
		_description = description;
		_overview = overview;
		_document = documents;
		_support = support;
		_tool = tools;
	}

	public String getDescription() {
		return _description;
	}

	public String getDocument() {
		return _document;
	}

	public String getMarketSector() {
		return _sector;
	}

	public String getModel() {
		return _model;
	}

	public String getName() {
		return _name;
	}

	public String getOverview() {
		return _overview;
	}

	public String getSupport() {
		return _support;
	}

	public String getTools() {
		return _tool;
	}

	public static Product getByModel(String model) {
		for(Product product : getProductList()) {
			if(product.getModel().equals(model))
			{
				return product;
			}
		}
		return null;
	}
	public void setDescription(String description) {
		_description = description;
	}

	public void setDocuments(String documents) {
		_document = documents;
	}

	public void setMarketSector(String marketSector) {
		_sector = marketSector;
	}

	public void setModel(String model) {
		_model = model;
	}

	public void setName(String name) {
		_name = name;
	}

	public void setOverview(String overview) {
		_overview = overview;
	}

	public void setSupport(String support) {
		_support = support;
	}

	public void setTools(String tools) {
		_tool = tools;
	}
}
